package com.xx.hdfs;

import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class HdfsLog {
    private final ListView<Text> view;
    public StackPane sp;
    private ArrayList<String> logInfo;

    public HdfsLog() {
        sp = new StackPane();
        view = new ListView<>();
        sp.getChildren().add(view);
    }

    //    日志等级从小到大依次分为,成功,异常,错误.
    public void addLog(int level, String info) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("   yyyy年MM月dd日 HH:mm:ss    ");
        info = dateFormat.format(System.currentTimeMillis()) + info;
        Text t = new Text();
        t.setFont(new Font(20));
        switch (level) {
            case 1: {
                info = "INFO    " + info;
                t.setFill(new Color(0.13, 0.51, 0.0, 1.0));
            }
            break;
            case 2: {
                info = "WARN " + info;
                t.setFill(new Color(0.78, 0.57, 0.05, 1.0));
            }
            break;
            case 3: {
                info = "ERROR " + info;
                t.setFill(new Color(0.73, 0.09, 0.09, 1.0));
            }
            break;
        }
        t.setText(info);
        logInfo.add(level + "");
        logInfo.add(info);
        view.getItems().add(t);
    }

    private void loadingLog(int level, String info) {
        Text t = new Text(info);
        t.setFont(new Font(20));
        switch (level) {
            case 1: {
                t.setFill(new Color(0.13, 0.51, 0.0, 1.0));
            }
            break;
            case 2: {
                t.setFill(new Color(0.78, 0.57, 0.05, 1.0));
            }
            break;
            case 3: {
                t.setFill(new Color(0.73, 0.09, 0.09, 1.0));
            }
            break;
        }
        view.getItems().add(t);
    }

    public void initView(ArrayList<String> strings) {
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) loadingLog(Integer.parseInt(iterator.next()), iterator.next());
        logInfo = strings;
    }

    public void save(Map map) {
        map.put("logInfo", logInfo);
    }
}
